package com.szalay.desktopsearch.ui;

import com.szalay.desktopsearch.indexer.Fields;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SpringLayout;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;

/**
 * Action to show the details of a search result document.
 * @author szalay
 */
final class ShowDetailsAction extends AbstractAction {
    private static final long serialVersionUID = -4377014619185080767L;

    private final SolrDocument document;
    private final QueryResponse response;
    private final JPopupMenu parent;

    ShowDetailsAction(SolrDocument document, QueryResponse response, JPopupMenu parent) {
        super("Details", new ImageIcon(ShowDetailsAction.class.getResource("/icons/info.png")));
        this.document = document;
        this.response = response;
        this.parent = parent;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        parent.setVisible(false);
        
        final JFrame frame = new JFrame(document.getFieldValue(Fields.PATH).toString());     
        frame.setIconImage(new ImageIcon(ShowDetailsAction.class.getResource("/icons/search.png")).getImage());        
        final JPanel content = new JPanel();
        frame.setSize(800, 600);
        final SpringLayout grid = new SpringLayout();
        content.setLayout(grid);
        
        
        final JLabel pathLabel = new JLabel("<html><b>Path</b></html>");
        final JLabel titleLabel = new JLabel("<html><b>Title</b></html>");
        final JLabel titleValue = new JLabel(getNullableValue(document.getFieldValue(Fields.TITLE)));
        final JLabel typeLabel = new JLabel("<html><b>Type</b></html>");
        final JLabel modifiedLabel = new JLabel("<html><b>Last modified</b></html>");
        final JLabel sizeLabel = new JLabel("<html><b>Size</b></html>");
        final JLabel sizeValue = new JLabel(renderSize(document.getFieldValue(Fields.SIZE).toString()));
        final JLabel textLabel = new JLabel("<html><b>Text</b></html>");
        final JLabel pathValue = new JLabel(document.getFieldValue(Fields.PATH).toString());
        final JLabel typeValue = new JLabel(getFirstValue(document.getFieldValue(Fields.MIMETYPE)));
        final JLabel modifiedValue = new JLabel(
                renderDate(getFirstDateValue(document.getFieldValue(Fields.LAST_MODIFIED_DATE))));
        final Map<String, List<String>> get = 
                response.getHighlighting().get(document.getFieldValue(Fields.PATH).toString());
        final String firstKey = get.keySet().iterator().next();
        final List<String> listForFirstKey = get.get(firstKey);
        final JLabel textValue = new JLabel("<html>" + listForFirstKey.get(0) + "</html>");
        content.add(pathLabel);
        content.add(pathValue);
        content.add(titleLabel);
        content.add(titleValue);
        content.add(typeLabel);
        content.add(typeValue);
        content.add(modifiedLabel);
        content.add(modifiedValue);
        content.add(sizeLabel);
        content.add(sizeValue);
        content.add(textLabel);
        content.add(textValue);
        UI.makeCompactGrid(content, 6, 2, 5, 5, 10, 10);
        
        UI.centerScreen(frame);
        frame.setLayout(new BorderLayout());
        frame.add(DetailsToolBar.getDetailWindowToolBar(document, parent), BorderLayout.NORTH);
        frame.add(content, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
    
    private String getFirstValue(final Object fieldValue) {
        @SuppressWarnings("unchecked")
        final List<Object> l = (List<Object>) fieldValue;
        return l.get(0).toString();
    }
    
    private String getNullableValue(final Object fieldValue) {
        if (fieldValue == null) {
            return "";
        }
        else {
            return fieldValue.toString();
        }
    }
    
    private Date getFirstDateValue(final Object fieldValue) {
        @SuppressWarnings("unchecked")
        final List<Object> l = (List<Object>) fieldValue;
        return (Date) l.get(0);
    }
    
    private String renderSize(final String input) {
        final Long number = Long.valueOf(input);
        return number + " bytes";
    }
    
    private String renderDate(final Date date) {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.ENGLISH);
        return format.format(date);
    }
}
